#include <zenoui/Game.hpp>
#include <zenoui/Rect.hpp>
#include <zenoui/widgets/EmptyWidget.hpp>
#include <zenoui/widgets/GraphicsItemWidget.hpp>
#include <zenoui/widgets/GraphicsViewWidget.hpp>
#include <zenoui/widgets/ButtonWidget.hpp>
#include <zenoui/widgets/LabelWidget.hpp>
#include <zenoui/widgets/TextInputWidget.hpp>
#include <zenoui/widgets/GraphicsGraph.hpp>
#include <zenox/unicode.hpp>
#include <zenoui/DOMBuilder.hpp>
#include <zenox/subroutine.hpp>

namespace zenoui {

static void build_game_ui() {
    auto &game = Game::instance();
    auto root = game.set_root_widget(DOMBuilder()
        .widget<GraphicsGraph>()
            .gview_scaling(2)
            .gview_translation(200, 200)
        .endw()
    .endDOM());
    auto gg = static_cast<GraphicsGraph *>(root->children_list().at(0));

    zenom::GraphModel *gm = new zenom::GraphModel();
    gg->set_model(gm);
    for (int i = 0; i < 3; i++) {
        auto nm = gm->new_node();
        nm->update(zenom::NodeRole::NodeType, (std::string)"hello");
        nm->update(zenom::NodeRole::NodeTitle, (std::string)"hellos");
        nm->update(zenom::NodeRole::NodePortCount, (size_t)3);
        nm->ports.at(0).update(zenom::PortRole::PortName, (std::string)"lhs");
        nm->ports.at(1).update(zenom::PortRole::PortName, (std::string)"mhs");
        nm->ports.at(2).update(zenom::PortRole::PortName, (std::string)"rhs");
        /* gm->delete_node(nm); */
    }
}

ZENO_SUBPROGRAM(ui)(int argc, char **argv) {
    (void)argc;
    (void)argv;

    auto &game = Game::instance();
    if (!game.init()) [[unlikely]] {
        return -1;
    }
#ifndef ZENO_HAS_SPLASH
    game.show_win();
#endif

    build_game_ui();

#ifdef ZENO_HAS_SPLASH
    int counter = 0;
    while (game.main_loop()) {
        if (counter++ > 2) {
            game.show_win();
            break;
        }
    }
#endif
    while (game.main_loop());
    return 0;
}

}
